//
//  LIULeftViewController.m
//  EasyNotes
//
//  Created by 劉裕 on 29/5/2016.
//  Copyright © 2016年 劉裕. All rights reserved.
//

#import "LIULeftViewController.h"
#import "LIUListViewController.h"
#import "LIUNavigationController.h"
#import <Masonry/Masonry.h>
#import "LIULeftViewCell.h"
#import "LIUMainViewController.h"
#import "LIUShareViewController.h"
#import "LIUAboutViewController.h"
#import "LIUTheme.h"

@interface LIULeftViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *leftTableView;
@property (nonatomic, strong) LIUNavigationController *otherNavigation;
@property (nonatomic, strong) LIUNavigationController *aboutNavigation;
@property (nonatomic, strong) LIUShareViewController *shareViewController;
@property (nonatomic, strong) LIUAboutViewController *aboutViewController;
@property (nonatomic, strong) NSArray *results;

@end

@implementation LIULeftViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.view.bounds = kScreenBounds;
    }
    return self;
}
#pragma mark - 懒加载

- (NSArray *)results {
    if (nil == _results) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"themes" ofType:@"plist"];
        _results = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *arrayM = [NSMutableArray array];
        for (NSDictionary *dict in _results) {
            LIUTheme *item = [LIUTheme itemWithDict:dict];
            [arrayM addObject:item];
        }
        _results = arrayM;
    }
    return _results;
}

- (LIUNavigationController *)navigation {
    if (nil == _navigation) {
        _navigation = [[LIUNavigationController alloc] initWithRootViewController:[LIUListViewController new]];
    }
    return _navigation;
}

- (LIUNavigationController *)otherNavigation {
    if (nil == _otherNavigation) {
        _otherNavigation = [[LIUNavigationController alloc] initWithRootViewController:self.shareViewController];
    }
    return _otherNavigation;
}

- (LIUShareViewController *)shareViewController {
    if (nil == _shareViewController) {
        _shareViewController = [LIUShareViewController new];
        _shareViewController.view.backgroundColor = [UIColor whiteColor];
    }
    return _shareViewController;
}

- (LIUNavigationController *)aboutNavigation {
    return [[LIUNavigationController alloc] initWithRootViewController:self.aboutViewController];
}

- (LIUAboutViewController *)aboutViewController {
    _aboutViewController = [LIUAboutViewController new];
    _aboutViewController.view.backgroundColor = [UIColor whiteColor];
    return _aboutViewController;
}

- (UITableView *)leftTableView {
    if (nil == _leftTableView) {
        _leftTableView = [[UITableView alloc] init];
        _leftTableView.backgroundColor = LIUColor(26, 31, 36, 1.0);
        _leftTableView.rowHeight = 50;
        [self.view addSubview:_leftTableView];
    }
    return _leftTableView;
}

#pragma mark - life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = LIUColor(26, 31, 36, 1.0);
    [self setupView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)setupView {
    
    WS(ws)
    [self.leftTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ws.view).offset(128);
        make.left.equalTo(ws.view).offset(0);
        make.right.equalTo(ws.view).offset(0);
        make.bottom.equalTo(ws.view).offset(-60);
    }];
    [self.leftTableView updateConstraintsIfNeeded];
    
    self.leftTableView.bounces = NO;
    self.leftTableView.delegate = self;
    self.leftTableView.dataSource = self;
    self.leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 默认第一个被选中
    [self.leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
}

#pragma mark - Table view delegate & datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.results.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LIULeftViewCell *cell = [LIULeftViewCell cellWithTableView:tableView];
    cell.item = self.results[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        [self.mainController setCenterViewController:self.navigation withCloseAnimation:YES completion:nil];
    } else if (indexPath.row == 1) {
        [self.mainController setCenterViewController:self.otherNavigation withCloseAnimation:YES completion:nil];
    } else {
        [self.mainController setCenterViewController:self.aboutNavigation withCloseAnimation:YES completion:nil];
    }
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

@end
